<!--test.vue-->
<template>
  <div style="width: 100%; height: 100%" ref="echartsBox">
    <v-chart ref="echarts" class="chart" :option="option" :key="key" />
  </div>
</template>

<script>
export default {
  name: "contrastBarChart",
  props: {
    contrastBarChartForm: {
      type: Object,
      default: () => {
        const Obj = {
          animationDuration: 500,
          animationDurationUpdate: 3000,
          tooltip: {
            trigger: "axis"
          },
          legend: {
            show: true,
            left: "left",
            top: "bottom",
            textStyle: {
              color: "rgba(214, 63, 63, 1)",
              fontWeight: "normal",
              fontSize: 14,
              fontFamily: "Arial"
            },
          },
          calculable: true,
          xAxis: {
            type: "category",
            show: true,
            axisLine: {
              lineStyle: {
                color: "#818285"
              }
            },
            axisLabel: {
              color: "#818285",
              fontWeight: "normal",
              fontFamily: "Microsoft Yahei",
              fontSize: 13
            },
            data: [
              "11月",
              "2月",
              "3月",
              "4月",
              "5月",
              "6月",
              "7月",
              "8月",
              "9月",
              "10月",
              "11月",
              "12月"
            ]
          },
          yAxis: {
            type: "value",
            show: true,
            min: "dataMin",
            max: "dataMax",
            splitNumber: 5,
            
            axisLine: {
              show: true,
              lineStyle: {
                color: "#999"
              }
            },
            axisLabel: {
              fontSize: 12,
              fontWeight: "normal",
              fontFamily: "Microsoft YaHei",
              color: "#000"
            },
            splitLine: {
              show: false,
              lineStyle: {
                color: "#000",
                type: "solid"
              }
            }
          },
          series: [
            {
              name: "系列一",
              type: "bar",
              data: [
                2.0,
                4.9,
                7.0,
                23.2,
                25.6,
                76.7,
                135.6,
                162.2,
                32.6,
                20.0,
                6.4,
                3.3
              ],
              itemStyle: {
                color: "#48e123"
              },
              barWidth: 10,
              label: {
                show: true,
                position: "top",
                color: "#818285",
                fontWeight: "normal",
                fontFamily: "Microsoft Yahei",
                fontSize: 13
              }
            },
            {
              name: "系列二",
              type: "bar",
              data: [
                2.6,
                5.9,
                9.0,
                26.4,
                28.7,
                70.7,
                175.6,
                182.2,
                48.7,
                18.8,
                6.0,
                2.3
              ],
              itemStyle: {
                color: "#0973ff"
              },
              barWidth: 10,
              label: {
                show: true,
                position: "top",
                color: "#818285",
                fontWeight: "normal",
                fontFamily: "Microsoft Yahei",
                fontSize: 13
              }
            }
          ]
        };
        return Obj;
      }
    }
  },
  watch: {
    contrastBarChartForm: {
      handler(val) {
        this.option = val;
      },
      deep: true,
      immediate: true
    }
  },
  data() {
    return {
      option: {},
      key: 1
    };
  },
  mounted() {
    let MutationObserver =
      window.MutationObserver ||
      window.WebKitMutationObserver ||
      window.MozMutationObserver;
    let element = this.$refs["echartsBox"];
    this.observer = new MutationObserver(mutationList => {
      for (let mutation of mutationList) {
        console.log(mutation);
      }
      console.log("自适应");
      // this.$refs['echarts'].resize()
      this.key += 1;
    });
    this.observer.observe(element, {
      attributes: true,
      attributeFilter: ["style"],
      attributeOldValue: true
    });
  },
  beforeDestroyed() {
    if (this.observer) {
      this.observer.disconnect();
      this.observer.takeRecords();
      this.observer = null;
    }
  }
};
</script>

<style lang="scss" scoped>
</style>
